package cn.idietrade.idietradeusermodule8004.controller;

import cn.idietrade.common.pojo.SecondAddress;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.idietradeusermodule8004.service.addressservice.SecondAddressService;
import cn.idietrade.idietradeusermodule8004.utils.EmailUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-12-22 15:18
 * @description
 */
@RestController
public class SecondAddressController {
    @Resource
    private SecondAddressService secondAddressService;

    //查询用户地址
    @RequestMapping("/queryAddressList")
    public ReturnResultData<List<SecondAddress>> queryAddressList(Integer userId){
        ReturnResultData<List<SecondAddress>> resultData = new ReturnResultData<>();
        try {
            List<SecondAddress> list = secondAddressService.queryAddres(userId);
            if (EmptyUtils.isNotEmpty(list)) {
                resultData.setCode(1);
                resultData.setMessage("当前登录用户地址查询成功");
                resultData.setData(list);
            } else {
                resultData.setCode(0);
                resultData.setMessage("当前登录用户地址查询失败");
                resultData.setData(null);
            }
        }catch (Exception e) {
            resultData.setCode(0);
            resultData.setMessage("当前登录用户地址查询异常");
            resultData.setData(null);
            e.printStackTrace();
        }
        return resultData;
    }
    //查询用户地址
    @RequestMapping("/addAddress")
    public ReturnResultData addAddress(SecondAddress secondAddress){
        ReturnResultData resultData = new ReturnResultData<>();
        try {
            int flag = secondAddressService.addAddres(secondAddress);
            if (flag>0) {
                resultData.setCode(1);
                resultData.setMessage("添加地址成功");
                resultData.setData(null);
            } else {
                resultData.setCode(0);
                resultData.setMessage("添加地址失败");
                resultData.setData(null);
            }
        }catch (Exception e) {
            resultData.setCode(0);
            resultData.setMessage("添加地址异常");
            resultData.setData(null);
            e.printStackTrace();
        }
        return resultData;
    }
}
